package cn.com.haopy.yue.ai.db.service.hos.impl;


import cn.com.haopy.yue.ai.db.entity.arisk.HOSSystem;
import cn.com.haopy.yue.ai.db.mapper.arisk.HOSSystemMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.HOSSystemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HOSSystemServiceImpl extends BaseServiceImpl<HOSSystemMapper, HOSSystem> implements HOSSystemService {
    @Override
    public Map<String, String> queryAll() {
        LambdaQueryWrapper<HOSSystem> queryWrapper = new LambdaQueryWrapper<>();

        return getSystemConfigMap(queryWrapper);
    }

    private Map<String, String> getSystemConfigMap(LambdaQueryWrapper<HOSSystem> queryWrapper) {
        List<HOSSystem> systemList = super.list(queryWrapper);
        Map<String, String> data = new HashMap<>();
        for (HOSSystem system : systemList) {
            data.put(system.getKeyName(), system.getKeyValue());
        }
        return data;
    }

    @Override
    public void updateConfig(Map<String, String> data) {
        for (Map.Entry<String, String> entry : data.entrySet()) {
            LambdaQueryWrapper<HOSSystem> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(HOSSystem::getKeyName, entry.getKey());

            HOSSystem system = new HOSSystem();
            system.setKeyName(entry.getKey());
            system.setKeyValue(entry.getValue());

            super.saveOrUpdate(system, queryWrapper);
        }
    }

    @Override
    public boolean addConfig(String key, String value) {
        HOSSystem system = new HOSSystem();
        system.setKeyName(key);
        system.setKeyValue(value);

        return super.save(system);
    }

    @Override
    public Map<String, String> listXiKangInt(String... keys) {
        LambdaQueryWrapper<HOSSystem> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(HOSSystem::getKeyName, keys);

        return getSystemConfigMap(queryWrapper);
    }

    @Override
    public Map<String, String> getConfigByKey(String[] keys) {
        LambdaQueryWrapper<HOSSystem> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(HOSSystem::getKeyName, keys);

        return getSystemConfigMap(queryWrapper);
    }
}
